﻿package rob.entity{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import rob.core.*;
	import rob.maths.*
	import rob.data.*;
	
	public class Bot extends Primitive3d{
		private var _anim:MovieClip;
		private var _initScale:Number;
		private var _shadowFilter:DropShadowFilter;
		// direction vector, motion magnitude, _del = _dr * _dir
		private var _dir:Vect3d, _dr:Number, _del:Vect3d;
		// 3 collision points
		private var _collide1:Point, _gCollide1:Point;
		private var _collide2:Point, _gCollide2:Point;
		private var _collide3:Point, _gCollide3:Point;
		// animation
		private var _frame:Number, _frameRate:Number, _numFrames:uint;
		// 
		private var _id:uint, _visible:Boolean;
		
		public function Bot(m:MovieClip):void{
			super();
			_id = World3d.instance.getNewBotId();
			_anim = m;
			_visible = true;
			
			// setup animation
			_numFrames = _anim.clip.totalFrames;
			_frame = Math.ceil(Math.random() * _numFrames);
			_anim.clip.gotoAndStop(_frame);
			_frameRate = .7;
		
			// setup collision points
			_collide1 = new Point(m.collide1.x,m.collide1.y);
			_collide2 = new Point(m.collide2.x,m.collide2.y);
			_collide3 = new Point(m.collide3.x,m.collide3.y);
			m.collide1.visible = m.collide2.visible = m.collide3.visible = false;
			// setup position
			_initScale = m.scaleX;
			position.x =  _anim.x;
			position.z =  _anim.y;
			// direction
			_dir = new Vect3d(0,0,1);
			_dr = 1.2;
			_del = VectorMaths.scaleNew(_dr,_dir);
			// setup shadows
			_shadowFilter = new DropShadowFilter(5,34,0x888888,1,0,0,1)
			//_anim.filters = [_shadowFilter];
			
			changeDirBy(2 * Math.PI * Math.random());
		}
		
		
		// change direction of bot by angle (radians)
		public function changeDirBy(dtheta:Number):void{
			VectorMaths.rotateBy(dtheta,_dir);
			_del = VectorMaths.scaleNew(_dr,_dir);
			_anim.rotation -= dtheta * (180 / Math.PI);
		}
				
		public function update(c:Camera3d,bnds:Sprite):void{
			// move
				VertexMaths.addTo(position,_del);
				VertexMaths.project(position,c);
				_anim.x = position.sx;
				_anim.y = position.sy;

			// check visiblity
			_visible = _anim.hitTestObject(bnds);
			// update global collision points
			updateGlobalCollide();

			// animate
			_frame += _frameRate;
			if(_frame >= 1 + _numFrames) _frame -= _numFrames ;
			_anim.clip.gotoAndStop(Math.floor(_frame));
			//_anim.body.gotoAndStop(Math.floor(_frame));
		}
				
		public function updateGlobalCollide():void{
			_gCollide1 = _anim.localToGlobal(_collide1);
			_gCollide2 = _anim.localToGlobal(_collide2);
			_gCollide3 = _anim.localToGlobal(_collide3);
		}
				
		public function setDimensions(k:Number){
			_initScale *= k;
			scaleBy(1);
		}
		
		public function rotateBy(dtheta:Number):void{
			_anim.rotation -= dtheta * (180 / Math.PI);
			//_shadowFilter.angle += dtheta;
			//anim.filters = [_shadowFilter];
		}
		
		public function scaleBy(scf:Number):void{
			_anim.scaleX = _anim.scaleY = _initScale * scf;
		}

		public function get gCollide1():Point{ return _gCollide1;}
		public function get gCollide2():Point{ return _gCollide2;}
		public function get gCollide3():Point{ return _gCollide3;}
		
		public function get anim():MovieClip{ return _anim;}
		public function get id():uint{ return _id;}
		public function get frame():uint{ return _frame;}
		public function get visible():Boolean{ return _visible;}

	}
}